package com.markus.code.exam.baidu;

import java.util.List;
import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/3 19:34
 * todo:
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int n = scanner.nextInt();
            int count_5 = 0;
            int count_0 = 0;
            for (int i=0;i<n;i++){
                int m = scanner.nextInt();
                if (m==0){
                    count_0++;
                } else{
                    count_5++;
                }
            }
            if (count_0 == 0 || count_5 < 9){
                System.out.println(-1);
            } else{
                int count = count_5 - count_5 % 9;
                StringBuilder sb = new StringBuilder();
                for (int i=0;i<count;i++){
                    sb.append(5);
                }
                for (int i=0;i<count_0;i++){
                    sb.append(0);
                }
                System.out.println(sb);
            }

        }
    }
}
